﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace XBoshiGFS.forms
{
    public partial class PeopleForm : Form
    {
    
        public   PeopleSet people { get; set; }
        public XBoshiGFSModelContainer context = new XBoshiGFSModelContainer();
        private bool isModify = false;


        public PeopleForm()
        {
            InitializeComponent();


            people = new PeopleSet();
            people.Id = Guid.NewGuid().ToString();
            context.AddToPeopleSet(people);

            peopleSetBindingSource.DataSource = people;

            cmbStation.DataSource = (from p in context.StationSet select p);
           // context.Attach(people); 
        }

        public PeopleForm(PeopleSet p_people)
        {
            InitializeComponent();
            isModify = true;//是修改用户信息
            
            
            this.people = (from p in context.PeopleSet where p.Id == p_people.Id select p).FirstOrDefault();// p_people;
            peopleSetBindingSource.DataSource = people;


            context.Attach(people); 

            var group = (from p in context.GroupSet where p.Id == people.GroupId select p).FirstOrDefault();
            cmbGroup.DataSource = new List<Object>() { group };
            cmbGroup.DisplayMember = "strName";
            cmbGroup.ValueMember = "Id";
            cmbGroup.SelectedValue = group.Id; 
            cmbGroup.Enabled = false;

            var village = group.VillageSet;
            cmbVillage.DataSource = new List<Object>() { village };
            cmbVillage.Enabled = false;
            cmbVillage.SelectedValue = village.Id;


            var watercourse = village.WaterCourseSet;
            cmbWaterCourse.DataSource = new List<Object>() { watercourse };
            cmbWaterCourse.Enabled = false;
            cmbWaterCourse.SelectedValue = watercourse.Id;

            var segment = watercourse.SegmentCourseSet;
            cmbSegment.DataSource = new List<Object>() { segment };
            cmbSegment.Enabled = false;
            cmbSegment.SelectedValue = segment.Id;


          //  var station = segment.StationSet; 
            cmbStation.DataSource = (from p in context.StationSet select p);
            cmbStation.DisplayMember = "strName";
            cmbStation.ValueMember = "id";
            cmbStation.Enabled = false;


        }

       



        private void cmbStation_SelectedIndexChanged(object sender, EventArgs e)
        {
           

            var strID=((ComboBox )sender).SelectedValue ;
            cmbSegment.DataSource = (from p in context.SegmentCourseSet where p.StationId == strID orderby p.strName  select p);


        }

        private void cmbSegment_SelectedIndexChanged(object sender, EventArgs e)
        {
            var strID = ((ComboBox)sender).SelectedValue;
            cmbWaterCourse.DataSource = (from p in context.WaterCourseSet where p.SegmentCourseId == strID orderby p.strName select p);

        }

        private void cmbWaterCourse_SelectedIndexChanged(object sender, EventArgs e)
        {
            var strID = ((ComboBox)sender).SelectedValue;
            cmbVillage.DataSource = (from p in context.VillageSet where p.WaterCourseId == strID orderby p.strName select p);

        }

        private void cmbVillage_SelectedIndexChanged(object sender, EventArgs e)
        {
             
            var strID = ((ComboBox)sender).SelectedValue;
            var list = (from p in context.GroupSet where p.VillageId == strID orderby p.strName select p).ToList();
            cmbGroup.DataSource = list;// (from p in context.GroupSet where p.VillageId == strID orderby p.strName select p).ToList();
          //  cmbGroup.DisplayMember = "strName";
     //       cmbGroup.Refresh(); 
        }

        private void cmbGroup_SelectedIndexChanged(object sender, EventArgs e)
        {

          //  return;
            
            //给当前用户赋组值
            var strID = ((ComboBox)sender).SelectedValue;
            if (strID == null) {
                return;
            
            }
            people.GroupId = strID.ToString(); ;
            
            //编号
            var code = (from p in context.PeopleSet where p.GroupId == strID group p by p.GroupId into g
                    select g.Max(p => p.strCode )
            ).FirstOrDefault() ;
            var strCode = "";
            if (code == "" || code == null)
            {
                strCode = "0";
            }
            else {
                strCode = (Int64.Parse(code) + 1).ToString(); 
            }
            if(isModify ==false){
                //修改用户，不变话
                people.strCode = strCode; 
            }

            //var strID = ((ComboBox)sender).SelectedValue;
            //cmbGroup.DataSource = (from p in context.GroupSet where p.VillageId == strID select p);

        }

        private void PeopleForm_Load(object sender, EventArgs e)
        {

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(people.GroupId ))
            {
                MessageBox.Show("请选择所在的村组！", "提示");
                return;
            }


            if (String.IsNullOrWhiteSpace(people.strName) ) {
                MessageBox.Show("请填写姓名！","提示");   
                return;
            }
            if (String.IsNullOrWhiteSpace(people.strCode))
            {
                MessageBox.Show("请填写编号！", "提示");
                return;
            }

            try
            {
                context.SaveChanges();
                context.Detach(people);  
                this.Close();
            }
            catch (Exception ex) {

                MessageBox.Show("保存失败！");
            } 

        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            try
            {
                context.Detach(people);
                context.DetectChanges();
                this.Close();
            }
            catch { }
        }

        private void PeopleForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                context.Detach(people);
                context.DetectChanges();

            }
            catch { 
            }
        }




    }
}
